var SiteUtil = function() {
    this.version = '1.0.0';
    this.isMobile = false;
    this._isInit = false;
    this._init();
    this._errorTipTimer;
    this._successTipTimer;
}
SiteUtil.prototype._init = function() {
    if (this._isInit) {
        return;
    }
    $('body').append(
        '<div class="errorTip" style="">' +
        '<div class="errorTip-ctn">' +
        '<span id="errorText">请求异常，请稍后重试</span>' +
        '</div>' +
        '</div>');

    // $('.btn-back').click(function() {
    //     history.go(-1);
    // });

    $('body').append(
        // '<div class="successTip"><span class="successBg"></span><img class="successImg" src="/resource/images/success.png"><span class="successText">支付成功</span></div>'
        '<div style="display:none" class="successTip"><img src="../../resources/images/success-toast.png" class="successImg"><span class="successTxt"></span></div>'
    )
    this._isInit = true;
};

SiteUtil.prototype.showErrMsg = function(msg) {
    var $erTip = $('.errorTip').show();

    $('#errorText').text(msg);
    clearTimeout(this._errorTipTimer);
    this._errorTipTimer = setTimeout(function() {
        $erTip.hide();
    }, 3000);
};

SiteUtil.prototype.showMsg = function(msg) {
    this.showErrMsg(msg);
};

SiteUtil.prototype.showSuccessTip = function(txt) {

    var $successTip = $('.successTip').show();
    $successTip.find('.successTxt').text(txt || '支付成功');

    clearTimeout(this._errorTipTimer);
    this._successTipTimer = setTimeout(function() {
        $successTip.hide();
    }, 3000);
};
SiteUtil.prototype.getCookie = function(name) {
    var arr = document.cookie.match(new RegExp('(^| )' + name + '=([^;]*)(;|$)'));
    if (arr !== null) {
        return decodeURIComponent(arr[2]);
    }
    return null;
};
SiteUtil.prototype.setCookie = function(name, value, path, expiresMinute) {
    var Minute = expiresMinute === undefined ? -1 : expiresMinute;
    var _expStr = '';
    if (Minute !== -1) {
        var exp = new Date();
        exp.setTime(exp.getTime() + Minute * 60 * 1000);
        _expStr = ';expires=' + exp.toGMTString();
    }

    var _path = '';

    if (path) {
        _path = ';path=' + path;
    }

    document.cookie = name + '=' + encodeURIComponent(value) + _path + _expStr;
};

$(function() {
    window.util = new SiteUtil();
    if (navigator.userAgent.match(/(iPhone|iPod|Android|ios)/i)) {
        util.isMobile = true;
    }
    if (util.isMobile) {
        // alert(window.screen.width);
        var viewport = document.querySelector("meta[name=viewport]");
        viewport.setAttribute("content", "user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, target-densitydpi=device-dpi");
    }
    $('body').addClass(util.isMobile ? 'mobile-site' : 'pc-site');
});
